package com.ypf.rpc.manager.register;

import com.ypf.rpc.nettyHandler.HeartBeatServerHandler;
import com.ypf.rpc.nettyHandler.RegisterHeartBeatServerHandler;
import com.ypf.rpc.nettyHandler.RpcQueryRegisterRequestMessageHandler;
import com.ypf.rpc.nettyHandler.RpcRegisterRequestMessageHandler;
import com.ypf.rpc.procotol.MessageCodecSharable;
import com.ypf.rpc.procotol.ProcotolFrameDecoder;
import com.ypf.rpc.registery.ServerRegistry;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 *
 */
public class RpcRegisterManager {
    private int port;
    NioEventLoopGroup worker = new NioEventLoopGroup();
    NioEventLoopGroup boss = new NioEventLoopGroup();
    ServerBootstrap bootstrap = new ServerBootstrap();

    public RpcRegisterManager(int port){
        this.port = port;

    }

    public void start(){
        // 日志
        LoggingHandler LOGGING = new LoggingHandler(LogLevel.DEBUG);

        // 自定义消息编解码器
        MessageCodecSharable MESSAGE_CODEC = new MessageCodecSharable();

        //  自定义服务注册请求消息处理器
        RpcRegisterRequestMessageHandler RPC_REGISTER_HANDLER = new RpcRegisterRequestMessageHandler();

        // 自定义服务查询请求消息处理器
        RpcQueryRegisterRequestMessageHandler RPC_QUERY_REGISTER_HANDLER = new RpcQueryRegisterRequestMessageHandler();

        // 自定义注册中心心跳处理器
        RegisterHeartBeatServerHandler REGISTER_HEARTBEAT_SERVER = new RegisterHeartBeatServerHandler();

        try {
            bootstrap.group(boss,worker)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast(new IdleStateHandler(10,0,0, TimeUnit.SECONDS)); // 监听服务端 10 秒内是否发送心跳
                            pipeline.addLast(new ProcotolFrameDecoder());  // 定长处理器
                            pipeline.addLast(MESSAGE_CODEC);
                            pipeline.addLast(LOGGING);
                            pipeline.addLast(REGISTER_HEARTBEAT_SERVER);
                            pipeline.addLast(RPC_REGISTER_HANDLER);
                            pipeline.addLast(RPC_QUERY_REGISTER_HANDLER);
                        }
                    });
            // 绑定端口
            Channel channel = bootstrap.bind(port).sync().channel();
            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.err.println("启动注册中心出错");
        }finally {
            worker.shutdownGracefully();
            boss.shutdownGracefully();
        }
    }


}
